var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    feedbackState: false,
    orderState: true,
    order: [],
    hiddenModal2: true,
    hiddenModal1: true,
    hiddenModal3: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    // 获取系统信息
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          height: res.screenHeight
        });
      }
    });
    that.alreadyShow();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this;
    if (app.globalData.number != "") {
      that.alreadyShow();
    }
  },

  alreadyShow: function () {
    let that = this
    var number = app.globalData.number;
    var url = "http://" + app.globalData.ipv4 + ":8080/order/findByWx?customerNumber=" + number;
    wx.request({
      url: url,
      method: 'GET',
      success: function (e) {
        that.setData({
          order: e.data
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    if (app.globalData.number != "") {
      that.alreadyShow();
      that.setData({
        orderState: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  login: function () {
    wx.navigateTo({
      url: '../../components/bindnumber/bindnumber',
    })
  },
  deleteOrder: function (e) {
    var that = this;
    var data = {
      "orderId": that.data.id,
      "state": {
        "stateName": "申请取消"
      },
      "orderReason": that.data.reason
    };
    var url = "http://" + app.globalData.ipv4 + ":8080/order/update";
    wx.request({
      url: url,
      data: data,
      method: "POST",
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data === 1) {
          wx.showToast({
            title: '删除成功',
            icon: 'none',
            duration: 1500
          })
          that.alreadyShow();
        }
      }
    })
  },
  showModal2: function (e) {
    this.setData({
      hiddenModal2: false,
      id: e.target.id
    })
  },
  model2confirm: function (e) {
    this.setData({
      hiddenModal2: true,
      reason: this.data.input
    })
    this.deleteOrder();
  },
  model2cancel: function (e) {
    this.setData({
      hiddenModal2: true
    })
  },
  model1confirm: function (e) {
    this.setData({
      hiddenModal1: true,
      comment: this.data.input
    })
    this.comment();
  },
  model1cancel: function (e) {
    this.setData({
      hiddenModal1: true
    })
  },
  showModal1: function (e) {
    let that = this;
    var url = "http://" + app.globalData.ipv4 + ":8080/comment/findByOrderId?orderId=" + e.target.id;
    wx.request({
      url: url,
      success: function (res) {
        console.log(res.data);
        if (res.data.length!=0) {
          if (res.data[0].commentState === "未回复") {
            wx.showToast({
              title: '已评论，请等待回复',
              icon: 'none',
              duration: 1500
            })
          } else {
            that.setData({
              hiddenModal3: false
            })
           that.findFeedback(res.data[0].commentId);
          }
        }else{
          that.setData({
            hiddenModal1: false,
            id1: e.target.id
          })
        }
      }
    })

  },
  comment: function (e) {
    var that = this;
    var id = that.data.id1;
    var data = {
      "commentContent": that.data.comment,
      "orders": {
        "orderId": id
      }
    };
    var url = "http://" + app.globalData.ipv4 + ":8080/comment/insert";
    wx.request({
      url: url,
      data: data,
      method: "POST",
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data === 1) {
          wx.showToast({
            title: '评价成功',
            icon: 'none',
            duration: 1500
          })
          that.alreadyShow();
        }
      }
    })
  },
  input: function (e) {
    this.setData({ input: e.detail.value })
  },
  model3cancel: function (e) {
    this.setData({
      hiddenModal3: true
    })
  },
  findFeedback: function(e){
    let that=this;
    var url = "http://" + app.globalData.ipv4 + ":8080/comment/findFeedback?commentId="+e;
    wx.request({
      url: url,
      success: function(res){
        that.setData({
          feedback: res.data
        })
        console.log(that.data.feedback);
      }
    })
  },
  checkOrder: function(e){
    var target=e.target.id;
    wx.navigateTo({
      url: '../../components/detail/detail?id=' + JSON.stringify(target),
    })
  }
})